$(document).ready(function() {
    // (1) 根据指定学生个数，逐个输入学生信息；
    // (2) 逐个显示学生表中所有学生的相关信息；
    // (3) 给定一个学生信息，插入到表中指定的位置；
    // (4) 删除指定位置的学生记录；
    // (5) 统计表中学生个数；
    // (6) 根据姓名（或者学号）进行顺序查找；
    // (7) 利用直接插入排序按照学号进行排序；
    // (8) 利用冒泡排序按照数学成绩进行排序；
    // (9) 利用简单选择排序按照语文成绩进行排序；
    // (10) 利用快速排序按照英语成绩进行排序；
    // (11) 根据学号进行折半查找，成功返回此学生的姓名和各科成绩。
    // "<tr><td></td><td><input type ='text'/> </td> <td> <input type = 'text'/> </td> <td > <input type = 'text' /> </td> <td > <input type = 'text'/> </td> <td > <input type = 'text' /> </td> <td > <button class = 'btn btn-info sure-add' > 确认添加 </button></td ></tr>";
    var stuNum;
    var MaxNum = 100; //数组最大值
    var students; //学生数组
    var result; //保存查找到的学生
    var result_location; //保存查找到的学生信息位置
    var column = 5; //列数

    //点击生成测试用例
    $('#test-case').on('click', function() {
        var stu1 = new Student("381152301", "Jack", 80, 99, 70);
        var stu2 = new Student("381152303", "Tom", 89, 79, 88);
        var stu3 = new Student("381152302", "Lucy", 54, 69, 73);
        var stu4 = new Student("381152306", "Sun", 80, 99, 53);
        var stu5 = new Student("381152305", "Rob", 79, 99, 89);
        var stu6 = new Student("381152304", "Jon", 66, 59, 77);
        var stuArray = new Array(MaxNum);
        stuArray[1] = stu1;
        stuArray[2] = stu2;
        stuArray[3] = stu3;
        stuArray[4] = stu4;
        stuArray[5] = stu5;
        stuArray[6] = stu6;
        students = new Students(6, stuArray);
        removeAndShowAll();
    });

    //点击初始化，获取学生数量并动态生成输入框
    $('#sure').on('click', function() {
        $('.stu-title').nextAll().remove();
        stuNum = $('.modal-body input').val();
        initStudents(stuNum);
        var html_code = "<tr><td></td><td><input type ='text'/> </td> <td> <input type = 'text'/> </td> <td > <input type = 'text' /> </td> <td > <input type = 'text'/> </td> <td > <input type = 'text' /> </td> <td > <button class = 'btn btn-info sure-add' > 确认添加 </button></td ></tr>";
        for (var i = 0; i < stuNum; i++) {
            $('.stu-title').after(html_code);
        }
    });

    //点击获取一行的学生信息并加入students
    var number = 0; //计数器
    $('table').delegate('.sure-add', 'click', function() {
        var sno = $(this).parents('tr').children().eq(1).children().val();
        var sname = $(this).parents('tr').children().eq(2).children().val();
        var math = $(this).parents('tr').children().eq(3).children().val();
        var chinese = $(this).parents('tr').children().eq(4).children().val();
        var english = $(this).parents('tr').children().eq(5).children().val();
        var stu = new Student(sno, sname, math, chinese, english);
        addStudent(stu);
        number = number + 1;
        $(this).parents('tr').remove();
        $('.table').append("<tr class='message'> <td class='location'>" + number + "</td><td >" + stu.sno + "</td><td>" + stu.sname + "</td><td>" + stu.math + "</td> <td>" + stu.chinese + "</td> <td>" + stu.english + "</td> <td> <button class = 'btn btn-danger delete-stu'> 删除 </button></td></tr>");
    });

    //点击显示所有学生
    $('#show-all').on('click ', function() {
        removeAndShowAll();
    });

    //点击插入学生信息
    $('#sure-insert').on('click', function() {
        $(document).ready(function() {
            var location = $('#location').val();
            var sno = $('#sno').val();
            var sname = $('#sname').val();
            var math = $('#math').val();
            var chinese = $('#chinese').val();
            var english = $('#english').val();
            var student = new Student(sno, sname, math, chinese, english);
            insertStudent(location, student);
            removeAndShowAll();
        });
    });

    //点击删除学生信息
    $('table').on('click', 'tr .delete-stu', function() {
        var location = $(this).parents('tr').children().eq(0).text();
        deleteStuByLocation(location);
        removeAndShowAll();
    });

    //点击进行顺序查找
    $('#sequential-search').on('click', function() {
        var condition = $('.select input').val();
        if (sequentialSearch(condition))
            removeAndShowResult(result);
        else {
            $(document).ready(function() {
                $('.stu-title').nextAll().remove();
                $('.table').append("<tr class='tip'><td>没有找到相关学生信息，请输入正确的查找条件</td></tr>");
            });
        }
    });

    //点击进行折半查找
    $('#bisearch').on('click', function() {
        var condition = $('.select input').val();
        var left = 1;
        var right = students.length;
        if (bisearch(condition, left, right))
            removeAndShowResult(result);
        else {
            $(document).ready(function() {
                $('.stu-title').nextAll().remove();
                $('.table').append("<tr class='tip'><td>没有找到相关学生信息，请输入正确的查找条件</td></tr>");
            });
        }
    });

    //点击进行直接插入排序
    $('#insert-sort').on('click', function() {
        insertSort();
        removeAndShowAll();
    });

    //点击进行冒泡排序
    $('#bubble-sort').on('click', function() {
        bubbleSort();
        removeAndShowAll();
    });

    //点击进行选择排序
    $('#select-sort').on('click', function() {
        selectSort();
        removeAndShowAll();
    });

    //点击进行快速排序
    $('#quick-sort').on('click', function() {
        quickSort(1, students.length);
        removeAndShowAll();
    });













    //数据展示
    //移除之前的html并展示所有
    function removeAndShowAll() {
        $(document).ready(function() {
            $('.stu-title').nextAll().remove();
            showStudents();
        });
    }

    //移除之前的html并展示查找结果
    function removeAndShowResult(stu) {
        $(document).ready(function() {
            $('.stu-title').nextAll().remove();
            $('.table').append("<tr class='message'> <td class='location'>" + result_location + "</td><td >" + stu.sno + "</td><td>" + stu.sname + "</td><td>" + stu.math + "</td> <td>" + stu.chinese + "</td> <td>" + stu.english + "</td> <td> <button class = 'btn btn-danger delete-stu'> 删除 </button></td></tr>");
        });
    }














    //数据结构方法
    //定义学生数组
    function Students(length, stuArray) {
        this.length = length;
        this.stuArray = stuArray;
    }

    //定义一个学生对象
    function Student(sno, sname, math, chinese, english) {
        this.sno = sno;
        this.sname = sname;
        this.math = math;
        this.chinese = chinese;
        this.english = english;
    }

    //初始化
    function initStudents() {
        var stuArray = new Array(MaxNum);
        students = new Students(0, stuArray);
    }

    //添加学生
    function addStudent(student) {
        students.stuArray[students.length + 1] = student;
        students.length++;
    }

    //展示所有学生信息
    function showStudents() {
        var stu;
        var num;
        for (var i = 1; i <= students.length; i++) {
            stu = students.stuArray[i]
            num = i;
            $('.table').append("<tr class='message'> <td class='location'>" + num + "</td><td >" + stu.sno + "</td><td>" + stu.sname + "</td><td>" + stu.math + "</td> <td>" + stu.chinese + "</td> <td>" + stu.english + "</td> <td> <button class = 'btn btn-danger delete-stu'> 删除 </button></td></tr>");
        }
    }

    //按位置插入学生信息
    function insertStudent(location, student) {
        for (var i = students.length + 1; i > location - 1; i--) {
            students.stuArray[i + 1] = students.stuArray[i];
        }
        students.stuArray[location] = student;
        students.length++;
    }

    //删除指定位置的学生信息
    function deleteStuByLocation(location) {
        for (var i = location; i < students.length + 1; i++) {
            students.stuArray[i] = students.stuArray[parseInt(i) + 1];
        }
        students.length--;
    }

    //根据姓名（或者学号）进行顺序查找
    function sequentialSearch(condition) {
        var stu;
        for (var i = 1; i <= students.length; i++) {
            stu = students.stuArray[i];
            if (stu.sno == condition || stu.sname == condition) {
                result = stu;
                result_location = i;
                return true;
            }
        }
        return false;
    }

    //根据学号进行折半查找，成功返回此学生的姓名和各科成绩
    function bisearch(condition, left, right) {
        if (left > right)
            return false;
        else {
            var mid = parseInt((left + right) / 2);
            console.log(mid);
            if (condition < students.stuArray[mid].sno)
                return bisearch(condition, left, mid - 1);
            if (condition > students.stuArray[mid].sno)
                return bisearch(condition, mid + 1, right);
            if (condition == students.stuArray[mid].sno) {
                result = students.stuArray[mid];
                result_location = mid + 1;
                return true;
            }
            return false;
        }
    }

    //利用直接插入排序按照学号进行排序
    function insertSort() {
        for (var i = 2; i <= students.length; i++) {
            if (students.stuArray[i].sno < students.stuArray[i - 1].sno) {
                students.stuArray[0] = students.stuArray[i];
                students.stuArray[i] = students.stuArray[i - 1];
                for (var j = i - 2; students.stuArray[0].sno < students.stuArray[j].sno; j--) {
                    students.stuArray[j + 1] = students.stuArray[j];
                }
                students.stuArray[j + 1] = students.stuArray[0];
            }
        }
    }

    //利用冒泡排序按照数学成绩进行排序
    function bubbleSort() {
        var m = students.length;
        var flag = 1;
        while (m > 1 && flag == 1) {
            flag = 0;
            for (j = 1; j < m; j++) {
                if (students.stuArray[j].math > students.stuArray[j + 1].math) {
                    flag = 1;
                    t = students.stuArray[j];
                    students.stuArray[j] = students.stuArray[j + 1];
                    students.stuArray[j + 1] = t;
                }
            }
            m--;
        }
    }

    //利用简单选择排序按照语文成绩进行排序
    function selectSort() {
        for (var i = 1; i < students.length; i++) {
            var k = i;
            var t;
            for (j = i + 1; j <= students.length; j++)
                if (students.stuArray[j].chinese < students.stuArray[k].chinese)
                    k = j;
            if (k != 1) {
                t = students.stuArray[i].chinese;
                students.stuArray[i].chinese = students.stuArray[k].chinese;
                students.stuArray[k].chinese = t;
            }
        }
    }

    //利用快速排序按照英语成绩进行排序
    function partition(low, high) {
        students.stuArray[0] = students.stuArray[low];
        var pivotkey = students.stuArray[low].english;
        while (low < high) {
            while (low < high && students.stuArray[high].english >= pivotkey)
                high--;
            students.stuArray[low] = students.stuArray[high];
            while (low < high && students.stuArray[low].english <= pivotkey)
                low++;
            students.stuArray[high] = students.stuArray[low];
        }
        students.stuArray[low] = students.stuArray[0];
        return low;
    }

    function quickSort(low, high) {
        var pivotloc;
        if (low < high) {
            pivotloc = partition(low, high);
            quickSort(low, pivotloc - 1);
            quickSort(pivotloc + 1, high);
        }
    }


});